/**
 *  AppManager.java        1.00 16 Nov, 2009
 *  
 *  Copyright (c) 2008 XProtean, Inc.
 *  461 S. Milpitas Blvd, Milpitas, CA, 95035, U.S.A.
 *  All rights reserved.
 * 
 *  This software is the confidential and proprietary information of 
 *  XProtean, Inc. ("Confidential Information").  You shall not
 *  disclose such Confidential Information and shall use it only in
 *  accordance with the terms of the license agreement you entered into
 *  with XProtean Inc.
 *   
 *  gprasad      1.00    16 Nov, 2009     gprasad     Initial code
 *
 */
package org.ql.client.framework;

import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.RootPanel;
import org.ql.client.util.Log;

/**
 *
 * @author gprasad
 */
public class AppManager {

    private static Frame sourceProviderFrame = null;
    private static boolean initialized = false;


    public static native String getContentOfFrame(Element elem)/*-{
    elem.onload = function(){ alert('x '+elem.contentWindow.document.body.innerHTML) };
    var D=elem.contentWindow.document;
    return D.body.innerHTML;
    }-*/;

    public static void init() {

        if (initialized) {
            return;
        }
        initialized = true;
        addSourceProviderFrame();
    }

    private static void addSourceProviderFrame() {
        sourceProviderFrame = new Frame();
        sourceProviderFrame.setVisible(false);
        sourceProviderFrame.setHeight("0");
        sourceProviderFrame.setWidth("0");

        RootPanel.get().add(sourceProviderFrame);
    }

    public static String getContent(String sourcePath) {
        sourceProviderFrame.setUrl(sourcePath);
        for (int i = 0; i < 100; i++) {
            String isLoaded = sourceProviderFrame.getElement().getAttribute("isloaded");
            Log.out("isLoaded : " + isLoaded);
        }
        Log.out("sourcePath " + sourcePath);
        Log.out("source : " + sourceProviderFrame.getElement());
        String srcHTML = "";

        srcHTML = getContentOfFrame(sourceProviderFrame.getElement());

        Log.out("source : " + srcHTML);
        return srcHTML;
    }
}
